import request from '@/utils/request/req';

// 获取客户管理列表
/**
 * 获取客户管理列表
 * @param params 查询参数
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: {
 *     id: string;
 *     wechatAccountId: string;
 *     myWxId: string;
 *     userOpenId: string;
 *     userWechatUserId: string;
 *     userName: string;
 *     userAvatarUrl: string;
 *     userEmail: string;
 *     userPhone: string;
 *     userCreateTime: string;
 *     userInfoUpdateTime: string;
 *     userStatus: string;
 *     remark: string;
 *     wxAccount: string;
 *     userAge: number;
 *     userBirthday: string;
 *     userQq: string;
 *     userAddress: string;
 *     userDescription: string;
 *     userCustomerSource: string;
 *     userAssignedEmployee: string;
 *     type: string; // 区分是否好友：1-好友，0-非好友
 *     purchaseRecordCnt: number; // 购买记录条数
 *     medicationReminderCnt: number; // 是否配置用药提醒（0-未配置，1-已配置）
 *   }[]
 * }>
 * @example
 * // 返回示例
 * {
 *   "code": 200,
 *   "msg": "操作成功",
 *   "data": [{
 *     "id": "user123",
 *     "wechatAccountId": "wx123",
 *     "myWxId": "mywx123",
 *     "userOpenId": "openid123",
 *     "userWechatUserId": "wxuser123",
 *     "userName": "张三",
 *     "userAvatarUrl": "http://example.com/avatar.jpg",
 *     "userEmail": "zhangsan@example.com",
 *     "userPhone": "13800138000",
 *     "userCreateTime": "2024-01-01 12:00:00",
 *     "userInfoUpdateTime": "2024-01-02 12:00:00",
 *     "userStatus": "active",
 *     "remark": "重要客户",
 *     "wxAccount": "wx_zhangsan",
 *     "userAge": 25,
 *     "userBirthday": "1999-01-01",
 *     "userQq": "123456789",
 *     "userAddress": "北京市朝阳区",
 *     "userDescription": "优质客户",
 *     "userCustomerSource": "自然流量",
 *     "userAssignedEmployee": "李四",
 *     "type": "1",
 *     "purchaseRecordCnt": 5,
 *     "medicationReminderCnt": 1
 *   }]
 * }
 */
export function getWechatUserList(params: {
  wxId?: string;
  userName?: string;
  userStatus?: string;
  remark?: string;
  wxAccount?: string;
  type?: string;
  current?: number;
  size?: number;
}) {
  return request({
    url: '/msg/wechatUser/list',
    method: 'get',
    params
  });
}

// 新增客户管理信息
/**
 * 新增客户管理信息
 * @param data 客户信息
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 * @example
 * // 返回示例
 * {
 *   "code": 200,
 *   "msg": "操作成功",
 *   "data": {
 *     "success": true
 *   }
 * }
 */
export function addWechatUser(data: {
  wechatUser: {
    wechatAccountId?: string;
    myWxId?: string;
    userOpenId?: string;
    userWechatUserId?: string;
    userName?: string;
    userAvatarUrl?: string;
    userEmail?: string;
    userPhone?: string;
    userStatus?: string;
    remark?: string;
    wxAccount?: string;
    userAge?: number;
    userBirthday?: string;
    userQq?: string;
    userAddress?: string;
    userDescription?: string;
    userCustomerSource?: string;
    userAssignedEmployee?: string;
    type: string; // 区分是否好友：1-好友，0-非好友
  };
}) {
  return request({
    url: '/msg/addWechatUser',
    method: 'post',
    data
  });
}

// 更新客户管理信息
/**
 * 更新客户管理信息
 * @param data 客户信息
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 * @example
 * // 返回示例
 * {
 *   "code": 200,
 *   "msg": "操作成功",
 *   "data": {
 *     "success": true
 *   }
 * }
 */
export function updateWechatUser(data: {
  wechatUser: {
    id: string;
    wechatAccountId?: string;
    myWxId?: string;
    userOpenId?: string;
    userWechatUserId?: string;
    userName?: string;
    userAvatarUrl?: string;
    userEmail?: string;
    userPhone?: string;
    userStatus?: string;
    remark?: string;
    wxAccount?: string;
    userAge?: number;
    userBirthday?: string;
    userQq?: string;
    userAddress?: string;
    userDescription?: string;
    userCustomerSource?: string;
    userAssignedEmployee?: string;
    type: string; // 区分是否好友：1-好友，0-非好友
  };
}) {
  return request({
    url: '/msg/updateWechatUser',
    method: 'post',
    data
  });
}

// 删除客户管理信息
/**
 * 删除客户管理信息
 * @param data 删除参数
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 * @example
 * // 返回示例
 * {
 *   "code": 200,
 *   "msg": "操作成功",
 *   "data": {
 *     "success": true
 *   }
 * }
 */
export function deleteWechatUser(data: {
  wechatUser: {
    id: string; // 当前数据id
    type: string; // 查询的时候会返回，区分是否好友：1-好友，2-非好友
    wxId: string; // 当前账号微信id
    sendWxId: string; // 要删除好友的微信id
  };
}) {
  return request({
    url: '/msg/deleteWechatUser',
    method: 'post',
    data
  });
}

// ==================== 购买记录相关接口 ====================

/**
 * 购买记录数据结构
 */
export interface PurchaseRecord {
  id?: number; // 购买记录主键ID
  wxId?: string; // 微信id
  myWxId?: string; // 当前登录用户的微信id
  shopName?: string; // 店铺名称+平台
  orderNo?: string; // 订单号
  receiverName?: string; // 收件人姓名
  receiverPhone?: string; // 收件人手机号
  receiverAddr?: string; // 收件人地址
  orderDate?: Date | string; // 下单日期
  productName?: string; // 产品名称
  productCode?: string; // 产品货号/编码
  productSpec?: string; // 购买规格
  quantity?: number; // 购买数量
  remark?: string; // 备注
}

/**
 * 购买记录查询参数
 */
export interface PurchaseRecordQueryParams {
  page?: any; // 分页对象
  wxId?: string; // 微信id
  myWxId?: string; // 当前登录用户的微信id
  shopName?: string; // 店铺名称+平台
  orderNo?: string; // 订单号
  receiverName?: string; // 收件人姓名
  receiverPhone?: string; // 收件人手机号
  orderDate?: Date | string; // 下单日期
  productName?: string; // 产品名称
  productCode?: string; // 产品货号/编码
  productSpec?: string; // 购买规格
  quantity?: number; // 购买数量
}

/**
 * 获取购买记录列表
 * @param params 查询参数
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: {
 *     records: PurchaseRecord[];
 *     total: number;
 *   }
 * }>
 */
export function getPurchaseRecordList(params: PurchaseRecordQueryParams) {
  return request({
    url: '/msg/purchaseRecord/list',
    method: 'get',
    params
  });
}

/**
 * 获取购买记录详情
 * @param id 购买记录主键ID
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: PurchaseRecord;
 * }>
 */
export function getPurchaseRecordDetail(id: number) {
  return request({
    url: `/msg/purchaseRecord/${id}`,
    method: 'get'
  });
}

/**
 * 新增购买记录
 * @param data 购买记录数据
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 */
export function addPurchaseRecord(data: PurchaseRecord) {
  return request({
    url: '/msg/purchaseRecord/add',
    method: 'post',
    data
  });
}

/**
 * 编辑购买记录
 * @param data 购买记录数据
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 */
export function updatePurchaseRecord(data: PurchaseRecord) {
  return request({
    url: '/msg/purchaseRecord/update',
    method: 'post',
    data
  });
}

/**
 * 删除购买记录
 * @param id 购买记录主键ID
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 */
export function deletePurchaseRecord(id: number) {
  return request({
    url: `/msg/purchaseRecord/delete/${id}`,
    method: 'post'
  });
}

// ==================== 用药提醒相关接口 ====================

/**
 * 用药提醒数据结构
 */
export interface MedicationReminder {
  id?: number; // 用药提醒主键ID
  wxId?: string; // 微信id
  sendWxId?: string; // 接收人微信id
  day?: number; // 间隔天数（每天传0，自定义传输入的数字）
  triple?: number; // 每天次数
  productName?: string; // 产品名称
  message?: string; // 消息内容
  status?: string; // 状态
  customerService?: string; // 客服微信
  customerServiceStatus?: string; // 是否通知客服（0 启用 1停用）
  deadline?: string; // 用药截至日期
  customerServiceName?: string; // 客服名称（逗号分隔）
  customerServiceList?: Array<{
    customerService: string; // 客服微信ID
    customerServiceName: string; // 客服名称
  }>; // 客服列表
}

/**
 * 用药提醒查询参数
 */
export interface MedicationReminderQueryParams {
  wxId?: string; // 微信id
  sendWxId?: string; // 接收人微信id
  day?: number; // 间隔天数（每天传0，自定义传输入的数字）
  triple?: number; // 每天次数
  productName?: string; // 产品名称
  message?: string; // 消息内容
  status?: string; // 状态
  customerService?: string; // 客服微信
  customerServiceStatus?: string; // 是否通知客服（0 启用 1停用）
  deadline?: string; // 用药截至日期
}

/**
 * 获取用药提醒form表单
 * @param params 查询参数
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: {
 *     records: MedicationReminder[];
 *     total: number;
 *   }
 * }>
 */
export function getMedicationReminderList(params: MedicationReminderQueryParams) {
  return request({
    url: '/msg/medicationReminder/list',
    method: 'get',
    params
  });
}

/**
 * 编辑用药提醒
 * @param data 用药提醒数据
 * @returns Promise<{
 *   code: number;
 *   msg: string;
 *   data: any;
 * }>
 */
export function updateMedicationReminder(data: MedicationReminder) {
  return request({
    url: '/msg/medicationReminder/update',
    method: 'post',
    data
  });
}
